package com.chen.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.chen.manager.service.ValidateCodeService;
import com.chen.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author cgj
 * @date 2/8/25 8:34 AM
 */
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public ValidateCodeVo generateValidateCode() {
//        生成验证码图片
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 20);
        String code = circleCaptcha.getCode(); // 验证码 当redis的值
        String imageBase64 = circleCaptcha.getImageBase64();  //设置编码
//        codeKey当redis的key
        String  codeKey= UUID.randomUUID().toString().replaceAll("-", "");
//       存入redis有效期5分钟
        redisTemplate.opsForValue().set("user:login:validateCode"+codeKey,code,5, TimeUnit.MINUTES);
//      设置放回对象内容
        ValidateCodeVo validateCodeVo = new ValidateCodeVo();
        validateCodeVo.setCodeKey(codeKey);
//      设置mime类型
        validateCodeVo.setCodeValue("data:image/png;base64,"+ imageBase64);
        return validateCodeVo;
    }
}
